<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JedisJedis介绍Jedis 是 Redis 官方首选的 Java 客户端开发包。 如何使用三样东西必不可少：  jdk redis-server java的redis驱动  我没有直接使用jar包导入，而是采用maven。 123456&lt;!-- https:&#x2F;&#x2F;mvnrepository.com&#x2F;artifact&#x2F;redis.clients&#x2F;jedis --&gt;&lt;depen">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令">
<meta property="og:url" content="http://example.com/2021/09/16/Redis%E5%9C%A8Java%20%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BD%BF%E7%94%A8%20%E2%80%94%E2%80%94%20%E4%BD%BF%E7%94%A8%20Jedis%20%E6%B5%8B%E8%AF%95%20redis%E6%93%8D%E7%BA%B5%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%91%BD%E4%BB%A4/index.html">
<meta property="og:site_name" content="JsyBlog">
<meta property="og:description" content="JedisJedis介绍Jedis 是 Redis 官方首选的 Java 客户端开发包。 如何使用三样东西必不可少：  jdk redis-server java的redis驱动  我没有直接使用jar包导入，而是采用maven。 123456&lt;!-- https:&#x2F;&#x2F;mvnrepository.com&#x2F;artifact&#x2F;redis.clients&#x2F;jedis --&gt;&lt;depen">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-15T16:00:00.000Z">
<meta property="article:modified_time" content="2021-12-01T02:19:48.025Z">
<meta property="article:author" content="SongyangJi">
<meta property="article:tag" content="NoSQL">
<meta property="article:tag" content="Redis">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/09/16/Redis%E5%9C%A8Java%20%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BD%BF%E7%94%A8%20%E2%80%94%E2%80%94%20%E4%BD%BF%E7%94%A8%20Jedis%20%E6%B5%8B%E8%AF%95%20redis%E6%93%8D%E7%BA%B5%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%91%BD%E4%BB%A4/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/09/16/Redis%E5%9C%A8Java%20%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BD%BF%E7%94%A8%20%E2%80%94%E2%80%94%20%E4%BD%BF%E7%94%A8%20Jedis%20%E6%B5%8B%E8%AF%95%20redis%E6%93%8D%E7%BA%B5%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%91%BD%E4%BB%A4/","path":"2021/09/16/Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令/","title":"Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令 | JsyBlog</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">JsyBlog</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Jedis"><span class="nav-number">1.</span> <span class="nav-text">Jedis</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Jedis%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.1.</span> <span class="nav-text">Jedis介绍</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8"><span class="nav-number">1.2.</span> <span class="nav-text">如何使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95string"><span class="nav-number">1.3.</span> <span class="nav-text">测试string</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95hash"><span class="nav-number">1.4.</span> <span class="nav-text">测试hash</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95list"><span class="nav-number">1.5.</span> <span class="nav-text">测试list</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%93%8D%E7%BA%B5set"><span class="nav-number">1.6.</span> <span class="nav-text">操纵set</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%93%8D%E7%BA%B5zset"><span class="nav-number">1.7.</span> <span class="nav-text">操纵zset</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95HyperLogLog"><span class="nav-number">1.8.</span> <span class="nav-text">测试HyperLogLog</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8B%E8%AF%95-Bitmap"><span class="nav-number">1.9.</span> <span class="nav-text">测试 Bitmap</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">SongyangJi</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">251</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">45</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">109</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/16/Redis%E5%9C%A8Java%20%E8%AF%AD%E8%A8%80%E7%9A%84%E4%BD%BF%E7%94%A8%20%E2%80%94%E2%80%94%20%E4%BD%BF%E7%94%A8%20Jedis%20%E6%B5%8B%E8%AF%95%20redis%E6%93%8D%E7%BA%B5%E5%87%A0%E7%A7%8D%E5%B8%B8%E8%A7%81%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%91%BD%E4%BB%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="SongyangJi">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="JsyBlog">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令 | JsyBlog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis在Java 语言的使用 —— 使用 Jedis 测试 redis操纵几种常见数据类型的命令
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-09-16 00:00:00" itemprop="dateCreated datePublished" datetime="2021-09-16T00:00:00+08:00">2021-09-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2021-12-01 10:19:48" itemprop="dateModified" datetime="2021-12-01T10:19:48+08:00">2021-12-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Redis/" itemprop="url" rel="index"><span itemprop="name">Redis</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Jedis"><a href="#Jedis" class="headerlink" title="Jedis"></a>Jedis</h1><h2 id="Jedis介绍"><a href="#Jedis介绍" class="headerlink" title="Jedis介绍"></a>Jedis介绍</h2><p>Jedis 是 Redis 官方首选的 Java 客户端开发包。</p>
<h2 id="如何使用"><a href="#如何使用" class="headerlink" title="如何使用"></a>如何使用</h2><p>三样东西必不可少：</p>
<ol>
<li>jdk</li>
<li>redis-server</li>
<li>java的redis驱动</li>
</ol>
<p>我没有直接使用jar包导入，而是采用maven。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- https://mvnrepository.com/artifact/redis.clients/jedis --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>redis.clients<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jedis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在终端打开 redis-server之后：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">PingJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//连接本地的 Redis 服务</span></span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        <span class="comment">// 如果 Redis 服务设置来密码，需要下面这行，没有就不需要</span></span><br><span class="line">        <span class="comment">// jedis.auth(&quot;123456&quot;);</span></span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        System.out.println(&quot;终止Redis服务:&quot;+jedis.shutdown());</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>如果正常返回 <code>pong</code>，说明我们已经可以在java里使用redis了；</p>
<h2 id="测试string"><a href="#测试string" class="headerlink" title="测试string"></a>测试string</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//       测试 set、get</span></span><br><span class="line">        jedis.set(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;Ji&quot;</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;name&quot;</span>));</span><br><span class="line">        <span class="comment">// 测试 get 不存在的键</span></span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;non-exist&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//       测试增量 incr incrBy</span></span><br><span class="line">        jedis.incr(<span class="string">&quot;num&quot;</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;num&quot;</span>));</span><br><span class="line">        jedis.incrBy(<span class="string">&quot;num&quot;</span>,<span class="number">2</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;num&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试增量 incr incrBy</span></span><br><span class="line">        jedis.decr(<span class="string">&quot;num&quot;</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;num&quot;</span>));</span><br><span class="line">        jedis.decrBy(<span class="string">&quot;num&quot;</span>,<span class="number">2</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;num&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试浮点数的增量</span></span><br><span class="line">        jedis.incrByFloat(<span class="string">&quot;num&quot;</span>,<span class="number">9.9</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;num&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试追加功能</span></span><br><span class="line">        jedis.append(<span class="string">&quot;name&quot;</span>,<span class="string">&quot; Songyang&quot;</span>);</span><br><span class="line">        System.out.println(jedis.get(<span class="string">&quot;name&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        获取字符串长度</span></span><br><span class="line">        System.out.println(jedis.strlen(<span class="string">&quot;name&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        一次性获取多个值</span></span><br><span class="line">        List&lt;String&gt; values = jedis.mget(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;num&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span>(String val:values) System.out.println(val);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line">Ji</span><br><span class="line"><span class="literal">null</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"><span class="number">9.90000000000000036</span></span><br><span class="line">Ji Songyang</span><br><span class="line"><span class="number">11</span></span><br><span class="line">Ji Songyang</span><br><span class="line"><span class="number">9.90000000000000036</span></span><br></pre></td></tr></table></figure>

<h2 id="测试hash"><a href="#测试hash" class="headerlink" title="测试hash"></a>测试hash</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HashInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 hset、hget</span></span><br><span class="line">        jedis.hset(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;color&quot;</span>,<span class="string">&quot;white&quot;</span>);</span><br><span class="line">        jedis.hset(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>,<span class="string">&quot;999998&quot;</span>);</span><br><span class="line">        System.out.println(jedis.hget(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;color&quot;</span>));</span><br><span class="line">        System.out.println(jedis.hget(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>));</span><br><span class="line"></span><br><span class="line">        Map&lt;String,String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">&quot;kind&quot;</span>,<span class="string">&quot;bmw&quot;</span>);</span><br><span class="line">        map.put(<span class="string">&quot;owner&quot;</span>,<span class="string">&quot;jsy&quot;</span>);</span><br><span class="line">        jedis.hset(<span class="string">&quot;car&quot;</span>,map);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//       批量获取 键、值、键值对</span></span><br><span class="line">        System.out.println(jedis.hlen(<span class="string">&quot;car&quot;</span>));</span><br><span class="line">        System.out.println(jedis.hgetAll(<span class="string">&quot;car&quot;</span>));</span><br><span class="line">        Set&lt;String&gt; keySet = jedis.hkeys(<span class="string">&quot;car&quot;</span>);</span><br><span class="line">        System.out.println(keySet);</span><br><span class="line">        List&lt;String&gt; values = jedis.hvals(<span class="string">&quot;car&quot;</span>);</span><br><span class="line">        System.out.println(values);</span><br><span class="line"></span><br><span class="line"><span class="comment">//       测试字段是否存在</span></span><br><span class="line">        System.out.println(jedis.hexists(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>));</span><br><span class="line">        System.out.println(jedis.hexists(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;manufacturer&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 hsetnx</span></span><br><span class="line">        jedis.hsetnx(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;country&quot;</span>,<span class="string">&quot;US&quot;</span>);</span><br><span class="line">        System.out.println(jedis.hgetAll(<span class="string">&quot;car&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试给某一个字段自增</span></span><br><span class="line">        jedis.hincrBy(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>,<span class="number">1</span>);</span><br><span class="line">        jedis.hincrByFloat(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>,<span class="number">0.99</span>);</span><br><span class="line">        System.out.println(jedis.hget(<span class="string">&quot;car&quot;</span>,<span class="string">&quot;price&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 删除字段</span></span><br><span class="line">        System.out.println(jedis.hdel(<span class="string">&quot;car&quot;</span>, <span class="string">&quot;price&quot;</span>, <span class="string">&quot;non-exist-field&quot;</span>));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line">white</span><br><span class="line"><span class="number">999998</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line">&#123;color=white, owner=jsy, price=<span class="number">999998</span>, kind=bmw&#125;</span><br><span class="line">[color, owner, price, kind]</span><br><span class="line">[white, <span class="number">999998</span>, bmw, jsy]</span><br><span class="line"><span class="literal">true</span></span><br><span class="line"><span class="literal">false</span></span><br><span class="line">&#123;owner=jsy, country=US, color=white, price=<span class="number">999998</span>, kind=bmw&#125;</span><br><span class="line"><span class="number">999999.98999999999068677</span></span><br><span class="line"><span class="number">1</span></span><br></pre></td></tr></table></figure>

<h2 id="测试list"><a href="#测试list" class="headerlink" title="测试list"></a>测试list</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ListInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//      测试 lpush、rpush</span></span><br><span class="line">        jedis.lpush(<span class="string">&quot;list&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;1&quot;</span>);</span><br><span class="line">        jedis.rpush(<span class="string">&quot;list&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;4&quot;</span>);</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试删除</span></span><br><span class="line">        jedis.lpop(<span class="string">&quot;list&quot;</span>);</span><br><span class="line">        jedis.rpop(<span class="string">&quot;list&quot;</span>);</span><br><span class="line">        System.out.println(jedis.llen(<span class="string">&quot;list&quot;</span>));</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">        jedis.lpop(<span class="string">&quot;list&quot;</span>);</span><br><span class="line">        jedis.lpop(<span class="string">&quot;list&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 此时 list为空</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">10</span>;i++) jedis.rpush(<span class="string">&quot;list&quot;</span>,<span class="string">&quot;&quot;</span>+i);</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 lrange(key,start,end);</span></span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">1</span>,<span class="number">2</span>));</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>,<span class="number">3</span>,-<span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//       测试线性表的get、set方法</span></span><br><span class="line">        System.out.println(jedis.lindex(<span class="string">&quot;list&quot;</span>,<span class="number">3</span>));</span><br><span class="line">        System.out.println(jedis.lindex(<span class="string">&quot;list&quot;</span>,-<span class="number">2</span>));</span><br><span class="line">        jedis.lset(<span class="string">&quot;list&quot;</span>,<span class="number">0</span>,<span class="string">&quot;10&quot;</span>);</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 list 的删除元素的功能</span></span><br><span class="line">        <span class="comment">// 按索引 保留 [start,end]</span></span><br><span class="line">        jedis.ltrim(<span class="string">&quot;list&quot;</span>,<span class="number">3</span>,<span class="number">8</span>);</span><br><span class="line">        <span class="comment">// 按元素去删</span></span><br><span class="line">        jedis.lrem(<span class="string">&quot;list&quot;</span>,<span class="number">1</span>,<span class="string">&quot;5&quot;</span>);</span><br><span class="line">        System.out.println(jedis.lrange(<span class="string">&quot;list&quot;</span>, <span class="number">0</span>, -<span class="number">1</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="number">2</span></span><br><span class="line">[<span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line">[<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line">[<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]</span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line">[<span class="number">10</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line">[<span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]</span><br></pre></td></tr></table></figure>

<h2 id="操纵set"><a href="#操纵set" class="headerlink" title="操纵set"></a>操纵set</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SetInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 sadd、srem</span></span><br><span class="line">        jedis.sadd(<span class="string">&quot;set&quot;</span>,<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;4&quot;</span>);</span><br><span class="line">        System.out.println(jedis.smembers(<span class="string">&quot;set&quot;</span>));</span><br><span class="line">        jedis.srem(<span class="string">&quot;set&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;5&quot;</span>);</span><br><span class="line">        System.out.println(jedis.smembers(<span class="string">&quot;set&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        元素是否在集合中</span></span><br><span class="line">        System.out.println(jedis.sismember(<span class="string">&quot;set&quot;</span>,<span class="string">&quot;2&quot;</span>));</span><br><span class="line">        System.out.println(jedis.sismember(<span class="string">&quot;set&quot;</span>,<span class="string">&quot;1&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        获取集合元素的个数</span></span><br><span class="line">        System.out.println(jedis.scard(<span class="string">&quot;set&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试集合的交集、并集、差集</span></span><br><span class="line">        <span class="comment">// 几个用于测试集合</span></span><br><span class="line">        jedis.sadd(<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>);</span><br><span class="line">        jedis.sadd(<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;1&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;4&quot;</span>);</span><br><span class="line">        jedis.sadd(<span class="string">&quot;set3&quot;</span>,<span class="string">&quot;2&quot;</span>,<span class="string">&quot;3&quot;</span>,<span class="string">&quot;6&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 并集</span></span><br><span class="line">        Set&lt;String&gt; res1 = jedis.sunion(<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        System.out.println(res1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 交集</span></span><br><span class="line">        Set&lt;String&gt; res2 = jedis.sinter(<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        System.out.println(res2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 差集</span></span><br><span class="line">        Set&lt;String&gt; res3 = jedis.sdiff(<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        System.out.println(res3);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 集合中间的元算，还可以存储到Redis中。</span></span><br><span class="line">        jedis.sunionstore(<span class="string">&quot;st1&quot;</span>,<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        jedis.sinterstore(<span class="string">&quot;st2&quot;</span>,<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        jedis.sdiffstore(<span class="string">&quot;st3&quot;</span>,<span class="string">&quot;set1&quot;</span>,<span class="string">&quot;set2&quot;</span>,<span class="string">&quot;set3&quot;</span>);</span><br><span class="line">        System.out.println(jedis.smembers(<span class="string">&quot;st1&quot;</span>));</span><br><span class="line">        System.out.println(jedis.smembers(<span class="string">&quot;st2&quot;</span>));</span><br><span class="line">        System.out.println(jedis.smembers(<span class="string">&quot;st3&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="literal">false</span></span><br><span class="line"><span class="literal">true</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">2</span>]</span><br><span class="line">[]</span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line">[<span class="number">2</span>]</span><br><span class="line">[]</span><br></pre></td></tr></table></figure>

<h2 id="操纵zset"><a href="#操纵zset" class="headerlink" title="操纵zset"></a>操纵zset</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"><span class="keyword">import</span> redis.clients.jedis.Tuple;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ZSetInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试有序集合的增加功能</span></span><br><span class="line">        jedis.zadd(<span class="string">&quot;student_score&quot;</span>,<span class="number">100</span>,<span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        Map&lt;String,Double&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">&quot;李四&quot;</span>,<span class="number">80.0</span>);</span><br><span class="line">        map.put(<span class="string">&quot;王五&quot;</span>,<span class="number">90.0</span>);</span><br><span class="line">        map.put(<span class="string">&quot;陈二&quot;</span>,<span class="number">60.0</span>);</span><br><span class="line">        jedis.zadd(<span class="string">&quot;student_score&quot;</span>,map);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        获取有序集合某个元素的分数、排名</span></span><br><span class="line">        System.out.println(jedis.zscore(<span class="string">&quot;student_score&quot;</span>,<span class="string">&quot;李四&quot;</span>));</span><br><span class="line">        System.out.println(jedis.zrank(<span class="string">&quot;student_score&quot;</span>,<span class="string">&quot;李四&quot;</span>));</span><br><span class="line">        System.out.println(jedis.zscore(<span class="string">&quot;student_score&quot;</span>,<span class="string">&quot;张三&quot;</span>));</span><br><span class="line">        System.out.println(jedis.zrank(<span class="string">&quot;student_score&quot;</span>,<span class="string">&quot;张三&quot;</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        按名次顺序检索元素（及其分数）</span></span><br><span class="line"></span><br><span class="line">        System.out.println();</span><br><span class="line">        System.out.println(<span class="string">&quot;按名次顺序检索元素（及其分数） ——————————————————————\n&quot;</span>);</span><br><span class="line">        <span class="comment">// 返回 索引 [0,2]的元素</span></span><br><span class="line">        System.out.println(jedis.zrange(<span class="string">&quot;student_score&quot;</span>,<span class="number">0</span>,<span class="number">2</span>));</span><br><span class="line">        <span class="comment">// 返回 索引 [1,3]的元素及分数</span></span><br><span class="line">        Set&lt;Tuple&gt; s = jedis.zrangeWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">1</span>,<span class="number">3</span>);</span><br><span class="line">        <span class="keyword">for</span>(Tuple tuple:s)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;元素： &quot;</span>+tuple.getElement()+<span class="string">&quot;      分数：&quot;</span>+tuple.getScore());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 降序返回</span></span><br><span class="line">        System.out.println(jedis.zrevrange(<span class="string">&quot;student_score&quot;</span>, <span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">        System.out.println(jedis.zrevrangeWithScores(<span class="string">&quot;student_score&quot;</span>, <span class="number">1</span>, <span class="number">3</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        按分数范围检索元素</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        System.out.println(<span class="string">&quot;按分数范围检索元素 ————————————————————  \n&quot;</span>);</span><br><span class="line">        <span class="comment">// 获取分数在 [80,100]的元素</span></span><br><span class="line">        System.out.println(jedis.zrangeByScore(<span class="string">&quot;student_score&quot;</span>,<span class="number">80</span>,<span class="number">100</span>));</span><br><span class="line">        <span class="comment">// 降序返回</span></span><br><span class="line">        System.out.println(jedis.zrevrangeByScore(<span class="string">&quot;student_score&quot;</span>, <span class="number">100</span>, <span class="number">80</span>));</span><br><span class="line">        <span class="comment">// 获取分数在 [80,100]的元素（包括分数）</span></span><br><span class="line">        System.out.println(jedis.zrangeByScoreWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">80</span>,<span class="number">100</span>));</span><br><span class="line">        <span class="comment">// 降序返回</span></span><br><span class="line">        System.out.println(jedis.zrevrangeByScoreWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">100</span>,<span class="number">80</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 返回分数大于等于70的，从第2个元素开始，且不超过2个元素，</span></span><br><span class="line">        System.out.println(jedis.zrangeByScoreWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">70</span>,Integer.MAX_VALUE,<span class="number">1</span>,<span class="number">2</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        返回有序集合的基数</span></span><br><span class="line">        System.out.println(jedis.zcard(<span class="string">&quot;student_score&quot;</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//        统计分数在 [min,max]的元素的个数</span></span><br><span class="line">        System.out.println(jedis.zcount(<span class="string">&quot;student_score&quot;</span>, <span class="number">75</span>, <span class="number">95</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//        测试 删除元素的功能</span></span><br><span class="line">        System.out.println(<span class="string">&quot;直接删除、按排名范围删除、按分数范围删除&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;当前有元素：  &quot;</span>+jedis.zrangeWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 直接删除</span></span><br><span class="line">        jedis.zrem(<span class="string">&quot;student_score&quot;</span>,<span class="string">&quot;张三&quot;</span>);</span><br><span class="line">        System.out.println(jedis.zrangeWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按照排名范围删除</span></span><br><span class="line">        jedis.zremrangeByRank(<span class="string">&quot;student_score&quot;</span>,<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line">        System.out.println(jedis.zrangeWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 按分数范围删除</span></span><br><span class="line">        jedis.zremrangeByScore(<span class="string">&quot;student_score&quot;</span>,<span class="number">70</span>,<span class="number">95</span>);</span><br><span class="line">        System.out.println(jedis.zrangeWithScores(<span class="string">&quot;student_score&quot;</span>,<span class="number">0</span>,-<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 有序集合的集合之间的操作 （ 并、交、差 ） 类似 集合之间的操作,这里略去</span></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line"><span class="number">80.0</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">100.0</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"></span><br><span class="line">按名次顺序检索元素（及其分数） ——————————————————————</span><br><span class="line"></span><br><span class="line">[陈二, 李四, 王五]</span><br><span class="line">元素： 李四      分数：<span class="number">80.0</span></span><br><span class="line">元素： 王五      分数：<span class="number">90.0</span></span><br><span class="line">元素： 张三      分数：<span class="number">100.0</span></span><br><span class="line">[张三, 王五, 李四]</span><br><span class="line">[[王五,<span class="number">90.0</span>], [李四,<span class="number">80.0</span>], [陈二,<span class="number">60.0</span>]]</span><br><span class="line"></span><br><span class="line">按分数范围检索元素 ————————————————————  </span><br><span class="line"></span><br><span class="line">[李四, 王五, 张三]</span><br><span class="line">[张三, 王五, 李四]</span><br><span class="line">[[李四,<span class="number">80.0</span>], [王五,<span class="number">90.0</span>], [张三,<span class="number">100.0</span>]]</span><br><span class="line">[[张三,<span class="number">100.0</span>], [王五,<span class="number">90.0</span>], [李四,<span class="number">80.0</span>]]</span><br><span class="line">[[王五,<span class="number">90.0</span>], [张三,<span class="number">100.0</span>]]</span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line">直接删除、按排名范围删除、按分数范围删除</span><br><span class="line">当前有元素：  [[陈二,<span class="number">60.0</span>], [李四,<span class="number">80.0</span>], [王五,<span class="number">90.0</span>], [张三,<span class="number">100.0</span>]]</span><br><span class="line">[[陈二,<span class="number">60.0</span>], [李四,<span class="number">80.0</span>], [王五,<span class="number">90.0</span>]]</span><br><span class="line">[[陈二,<span class="number">60.0</span>], [王五,<span class="number">90.0</span>]]</span><br><span class="line">[[陈二,<span class="number">60.0</span>]]</span><br></pre></td></tr></table></figure>


<h2 id="测试HyperLogLog"><a href="#测试HyperLogLog" class="headerlink" title="测试HyperLogLog"></a>测试HyperLogLog</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> redis.clients.jedis.Jedis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashSet;</span><br><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HyperLogLog</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span>+jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 随机插入 1E8 个数字，看一下 HyperLogLog 的准确率</span></span><br><span class="line">        <span class="type">Random</span> <span class="variable">random</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line"></span><br><span class="line">        <span class="type">int</span> <span class="variable">N</span> <span class="operator">=</span> (<span class="type">int</span>)<span class="number">1e6</span>;</span><br><span class="line">        <span class="type">int</span> <span class="variable">gap</span> <span class="operator">=</span> (<span class="type">int</span>)<span class="number">1e5</span> , n = (<span class="type">int</span>)<span class="number">1e5</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span>(n&lt;=N)&#123;</span><br><span class="line">            Set&lt;Integer&gt; set = <span class="keyword">new</span> <span class="title class_">HashSet</span>&lt;&gt;();</span><br><span class="line">            jedis.del(<span class="string">&quot;hp&quot;</span>);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;n;i++)&#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> random.nextInt();</span><br><span class="line">                set.add(x);</span><br><span class="line">                jedis.pfadd(<span class="string">&quot;hp&quot;</span>,Integer.toString(x));</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            System.out.println(<span class="string">&quot;真实数据是 &quot;</span>+set.size()+<span class="string">&quot;个不同的数&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;HyperLogLog 统计数据为 &quot;</span>+jedis.pfcount(<span class="string">&quot;hp&quot;</span>)+<span class="string">&quot;个不同的数&quot;</span>);</span><br><span class="line">            <span class="type">double</span> <span class="variable">rate</span> <span class="operator">=</span> <span class="number">1.0</span>*Math.abs(jedis.pfcount(<span class="string">&quot;hp&quot;</span>)-set.size())/set.size();</span><br><span class="line">            System.out.println(<span class="string">&quot;误差率为：&quot;</span>+rate);</span><br><span class="line">            System.out.println();</span><br><span class="line">            n += gap;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">连接成功</span><br><span class="line">服务正在运行: PONG</span><br><span class="line">——————————————————————————————————————————</span><br><span class="line">真实数据是 <span class="number">99999</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">100268</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.002690026900269003</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">199997</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">200718</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.003605054075811137</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">299989</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">297310</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.008930327445339663</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">399981</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">394993</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.012470592353136774</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">499974</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">497927</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.004094212899070752</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">599960</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">592881</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.011799119941329423</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">699948</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">697609</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.003341676810277335</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">799939</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">797154</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.0034815154655542485</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">899907</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">912082</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.01352917579260968</span></span><br><span class="line"></span><br><span class="line">真实数据是 <span class="number">999889</span>个不同的数</span><br><span class="line">HyperLogLog 统计数据为 <span class="number">998794</span>个不同的数</span><br><span class="line">误差率为：<span class="number">0.0010951215584929926</span></span><br><span class="line"></span><br><span class="line">Process finished with exit code <span class="number">0</span></span><br><span class="line">  </span><br></pre></td></tr></table></figure>



<h2 id="测试-Bitmap"><a href="#测试-Bitmap" class="headerlink" title="测试 Bitmap"></a>测试 Bitmap</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">BitMapInJedis</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Jedis</span> <span class="variable">jedis</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">&quot;localhost&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;连接成功&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;服务正在运行: &quot;</span> + jedis.ping());</span><br><span class="line">        System.out.println(<span class="string">&quot;——————————————————————————————————————————&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        先清空一下</span></span><br><span class="line">        jedis.flushAll();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">final</span> <span class="type">String</span> <span class="variable">key</span> <span class="operator">=</span> <span class="string">&quot;key_bitmap&quot;</span>;</span><br><span class="line"></span><br><span class="line">        jedis.setbit(key,<span class="number">0</span>,<span class="literal">true</span>);</span><br><span class="line">        jedis.setbit(<span class="string">&quot;key_bitmap&quot;</span>,<span class="number">1</span>,<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(key+<span class="number">1</span>+<span class="string">&quot; :&quot;</span>+jedis.getbit(key, <span class="number">1</span>));</span><br><span class="line">        System.out.println(key+<span class="number">2</span>+<span class="string">&quot; : &quot;</span>+jedis.getbit(key, <span class="number">2</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 只允许 0、1</span></span><br><span class="line">        jedis.setbit(key,<span class="number">131</span>,<span class="string">&quot;1&quot;</span>);</span><br><span class="line">        jedis.setbit(key,<span class="number">132</span>,<span class="string">&quot;0&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/NoSQL/" rel="tag"># NoSQL</a>
              <a href="/tags/Redis/" rel="tag"># Redis</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/09/16/Redis%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3%E7%AC%94%E8%AE%B0%20%E2%80%94%E2%80%94%20%E4%BA%8B%E5%8A%A1%EF%BC%88Transactions%EF%BC%89/" rel="prev" title="Redis官方文档笔记 —— 事务（Transactions）">
                  <i class="fa fa-chevron-left"></i> Redis官方文档笔记 —— 事务（Transactions）
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/09/23/%E5%A6%82%E4%BD%95%E8%A7%A3%E6%9E%90c%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F%E7%9A%84%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0%EF%BC%9F%E2%80%94%E2%80%94-Argtable3%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/" rel="next" title="如何解析c语言程序的命令行参数？—— Argtable3使用教程">
                  如何解析c语言程序的命令行参数？—— Argtable3使用教程 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">SongyangJi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  





</body>
</html>
